package com.lovo.pis.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lovo.pis.constant.MessageCode;
import com.lovo.pis.dto.PageBean;
import com.lovo.pis.dto.PowerBean;
import com.lovo.pis.dto.RoleBean;
import com.lovo.pis.dto.UserBean;
import com.lovo.pis.service.IRoleService;
import com.lovo.pis.service.IUserService;
import com.lovo.pis.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    IRoleService roleService;

    //分页查询所有角色
    @RequestMapping("/queryAll")
    public IPage<RoleBean> queryAll(@RequestBody PageBean<RoleBean> pageInfo){
        return roleService.queryAll(pageInfo);
    }

    //查询所有角色
    @RequestMapping("/queryAllRole")
    public List<RoleBean> queryAllRoleName(){
        return roleService.queryAllRoleName();
    }

    //查询多个角色的所有权限名称
    @RequestMapping("/queryPower")
    public List<PowerBean> queryPowerName(@RequestBody List<RoleBean> roleList){
        List<Integer> roleIdList = new ArrayList<>();
        roleList.forEach(a->roleIdList.add(a.getId()));
        return roleService.queryPowerName(roleIdList);
    }

    //删除角色
    @RequestMapping("/remove")
    public JsonResult removeById(int id) {
        JsonResult jsonResult = new JsonResult<>();
        roleService.removeById(id);
        return jsonResult;
    }

    //查看角色权限
    @RequestMapping("/query")
    public RoleBean queryById(int id) {
        return roleService.queryById(id);
    }

    //添加新用户
    @RequestMapping("/add")
    public JsonResult addRole(@RequestBody RoleBean roleBean){
        JsonResult jsonResult = new JsonResult<>();
        try{
            roleService.addRole(roleBean);
        }catch (Exception e){
            jsonResult.setCode(MessageCode.ERROR.getCode());
            jsonResult.setMessage(MessageCode.ERROR.getMessage());
        }
        return jsonResult;
    }


    //修改角色权限
    @RequestMapping("/update")
    public String changeById(@RequestBody RoleBean roleBean){
        return roleService.changeById(roleBean)?"success":"error";
    }


}
